<?php

namespace app\api\controller;

use think\Db;
use app\api\model\Task;
use app\api\model\ConfigSystem as Config;
use think\Cache;
use app\service\FaceAuth;

class Upload extends Common
{
    // 上传照片
    public function upload()
    {
        $user_id = $this->member['id'];
        $image = input('photo_album');
        $cover = input('cover') ?: '';
        $burn = input('burn') ?: 0;
        $packet = input('packet') ?: 0;
        $types = input('types/d') ?: 1;
        $face = input('face', 0);

        if (!$image) {
            return json_encode(['code' => 400, 'msg' => '未上传图片']);
        }
        
        $config = Config::index('check');

        $status = $config['check_photo'] ? 0 : 1;
        
        if ($status) {
            Cache::rm('photos' . $user_id);
            Cache::rm('photosCount' . $user_id);
        }
    
        $images = array_filter(explode(',', $image));
    
        if ($images) {
            /*if ($types == 1 && $burn == 0 && $packet == 0 && $status) {
                $city = Db::name('user')->where(['id' => $user_id])->value('city');
        
                $dynamic_id = Db::name('dymanic')->insertGetId([
                    'type' => 4,
                    'userid' => $user_id,
                    'sumbImage' => implode(',', $images),
                    'masterimg' => implode(',', $images),
                    'citys' => $city ?: '',
                    'shenhe_time' => time(),
                ]);
            }*/
    
            $current_time = time();
            $userImg = [];
            if ($face){
                $FaceAuth = new FaceAuth;
                $authpic = Db::name('auth')->where(['user_id'=>$user_id])->value('image1');
                if(!$authpic){
                    return json_encode(['code' => 400, 'msg' => '未真人认证，无法上传人脸匹配图片到相册']);
                }
                $path = getenv('APP_OSS') ? 'http://' . getenv('OSS_BUCKET') . '.' . getenv('OSS_ENDPOINT') : SITE_PATH;

                $authpic = (strstr(PHP_OS, 'WIN') ? 'http://' . getenv('APP_URL') : $path) . $authpic;

            }
            $faceVaildBlock = 0;
            foreach ($images as $image) {
                $photo = [
                    'user_id' => $user_id,
                    'pic' => stripslashes(json_encode([$image])),
                    'picthumb' => stripslashes(json_encode([$image])),
                    'burn' => $burn,
                    'packet' => $packet,
                    'cover' => $cover,
                    'types' => $types,
                    'status' => $status,
                    'is_face' => 0
                ];
    
                if ($types == 1 && $burn == 0 && $packet == 0) {
                    $photo['remark'] = $current_time;
                    $photo['join_dynamic'] = $status && isset($dynamic_id) ? $dynamic_id : '';
                }
                if ($face){
                    try {
                        $FAres = $FaceAuth->compare($path . $image, $authpic);
                    } catch (\Exception $e) {
                        $FAres = true;
                    }
                    if($FAres){
                        $faceVaildBlock++;
                    }else{
                        $photo['is_face'] = 1;
                    }
                }
    
                $userImg[] = $photo;
            }
    
            Db::name('user_img')->insertAll($userImg);
    
            $count = Db::name('user_img')->where(['user_id' => $user_id])->count();
    
            // 任务
            Task::finish('photo', $user_id, $count);
        }

        $tip = '';
        if($faceVaildBlock){
            $tip = ',其中' . $faceVaildBlock . '张未通过人脸匹配';
        }
        
        return json_encode(['code' => 200, 'msg' => $status == 0 ? '上传成功，待审核后方可显示' . $tip : '上传成功' . $tip]);
    }
}